from django import forms
from django.core.validators import RegexValidator
from django.shortcuts import render

from app01 import models


# Create your views here.

class RegisterForm(forms.ModelForm):
    mobile = forms.CharField(label="手机号",
                             max_length=20,
                             validators=[RegexValidator(regex=r'^1[3-9]\d{9}$',message="手机号格式错误")])
    password = forms.CharField(label="密码",widget=forms.PasswordInput)
    confirm_password = forms.CharField(label="重复密码",widget=forms.PasswordInput)
    code = forms.CharField(label="验证码",widget=forms.TextInput)

    class Meta:
        model = models.UserInfo
        fields = '__all__'

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        for name,field in self.fields.items():
            field.widget.attrs.update({
                'class': 'form-control',
                'placeholder': f'请输入{field.label}'
            })


def register(request):
    form = RegisterForm()
    return render( request, 'register.html', {'form': form})
